package com.sduept.nwld.dataserver.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.manager.RequestUnfinishedTaskManager;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/dataSourceWarnServices/")
public class DataSourceWarnServices {
	private static final String MSG_SUCCESS = "success";
	private static final String MSG_ERROR = "error";

	@Autowired
	private RequestUnfinishedTaskManager warnManager;


	/**
	 * 根据时间，查询t_osb_project（osb库中的）大分类告警正确率
	 * @param startDate 开始时间
	 * @param endDate  结束时间
	 * @return
	 * 1.通过t_osb_project 的id，关联t_request_configure 表中的pid。
	 * 2.生成一个Map<t_osb_project.name , t_request_configure.id（requestId）)>
	 * 3.加载整个页面时，取map中的所有key，然后根据task表中数据，算出可用率
	 */

	@GetMapping("/getOSBProjectByStartAndEndTime/{start}/{end}")
	public String getOSBProjectByStartAndEndTime(@PathVariable("start") String startDate,@PathVariable("end") String endDate) {
		JSONObject obj = new JSONObject();
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(d);
		if (dateNowStr.equals(endDate)) {
			Calendar now = Calendar.getInstance();
			String time = " " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
			endDate += time;
		} else {
			endDate += " 23:59:59";
		}
		Date s = DateUtil.getDateByString(startDate, DateUtil.pattern_ymd);
		Date e = DateUtil.getDateByString(endDate, DateUtil.pattern_ymdhms);
		// List<map<object名称，t_request_configure.id（requestId）>>
		List<Map<String, Object>> map = warnManager.getDataSourceWarnsGroupByOSBName(s, e);
		return obj.toJSONString(map);
	}

	/**
	 * 点击事件查看详情
	 * @param osbName
	 * @param startDate
	 * @param endDate
	 * @return
	 */

	@GetMapping("/getDetailTasks/{osbName}/{start}/{end}")
	public String getDetailTasks(@PathVariable("osbName") String osbName,@PathVariable("start") String startDate,@PathVariable("end") String endDate) {
		JSONObject obj = new JSONObject();
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(d);
		if (dateNowStr.equals(endDate)) {
			Calendar now = Calendar.getInstance();
			String time = " " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
			endDate += time;
		} else {
			endDate += " 23:59:59";
		}
		Date s = DateUtil.getDateByString(startDate, DateUtil.pattern_ymd);
		Date e = DateUtil.getDateByString(endDate, DateUtil.pattern_ymdhms);
		List<Map<String, String>> map = warnManager.getDetailsTaskByOSBName(osbName, s, e);
		return obj.toJSONString(map);
	}


}
